<!DOCTYPE html>
<html lang="en" xmlns:th="http://www.thymeleaf.org">
<head>
    <meta charset="UTF-8">
    <meta name="renderer" content="webkit">
    <meta http-equiv="X-UA-Compatible" content="IE=edge,chrome=1">
    <meta name="viewport" content="width=device-width, initial-scale=1, maximum-scale=1">
    <title>商品列表</title>
    <link rel="stylesheet" type="text/css" th:href="@{/js/layui/css/layui.css}" media="all">
    <link rel="stylesheet" type="text/css" th:href="@{/css/fyn/style.css/}" media="all">
    <link rel="stylesheet" type="text/css" th:href="@{/css/fyn/my.css/}" media="all">

</head>
<body class="body">

<script type="text/html" id="topOperate">
    <div class="my-btn-box layui-form">
        <span class="fl">
        <button class="layui-btn layui-btn-sm" lay-event="toSave"><i
        class="layui-icon"></i>添加</button>
        </span>
    </div>
</script>

<div class="my-btn-box layui-form">
    <span class="fr">
        <div class="layui-input-inline">
            <input type="text" autocomplete="off" name="title" placeholder="商品标题" class="layui-input height33">
        </div>
        <div class="layui-input-inline">
             <select name="authentication" id="saleId" lay-filter="state">
                <option value="">请选择是否下架</option>
                <option value="0">未下架</option>
                <option value="1">已下架</option>
              </select>
        </div>
        <div class="layui-input-inline">
             <select name="authentication" id="statusId" lay-filter="state">
                <option value="">请选择是否有效</option>
                <option value="0">有效</option>
                <option value="1">失效</option>
              </select>
        </div>
        <button class="layui-btn layui-btn-sm" id="search"><i class="layui-icon">&#xe615;</i></button>
    </span>
</div>

<!-- 表格 -->
<div id="dateTable" lay-filter="event"></div>
<div class="height50"></div>
<script th:src="@{/js/layui/layui.js}"></script>
<script th:src="@{/js/fyn/index.js}"></script>
<script th:src="@{/js/fyn/my.js}"></script>
<script type="text/javascript" th:src="@{/js/vip/vip_comm.js}"></script>
<script type="text/javascript" th:src="@{/js/vip/vip_table.js}"></script>
<script type="text/javascript" th:inline="javascript">

    // layui方法
    layui.use(['table', 'form', 'laypage', 'layer', 'vip_table'], function () {

        // 操作对象
        var form = layui.form
            , table = layui.table
            , layer = layui.layer
            , vipTable = layui.vip_table
            , $ = layui.jquery;

        // 表格渲染
        var tableIns = table.render({
            elem: '#dateTable'                  //指定原始表格元素选择器（推荐id选择器）
            , height: "full"    //容器高度
            // , skin: 'line'
            , even: 'true'
            , cols: [[                  //标题栏
                {field: 'title', title: '商品名称', minWidth: '80', align: 'center'},
                {field: 'description', title: '商品描述', minWidth: '80', align: 'center'},
                {field: 'imageUrl', title: '商品图片', minWidth: '80', align: 'center', templet: function (d) {
                if (d.imageUrl) {
                    return "<img style=\"width: 50px;height: 50px;\" src=\""+d.imageUrl+"\" class=\"layui-upload-img\">";
                }
                return "";
            }},
                {field: 'points', title: '积分', minWidth: '80', align: 'center'},
                {field: 'sale', title: '上架', minWidth: '80', align: 'center', templet: function (d) {
                        if (d.sale == 1) {
                            return "<button class='layui-btn layui-btn-danger layui-btn-xs'>下架</button>";
                        }
                        return "<button class='layui-btn layui-btn-normal layui-btn-xs'>上架</button>";
                }},
                {
                    field: 'status', title: '状态', align: 'center', minWidth: '100', templet: function (d) {
                        if (d.status == 1) {
                            return "<button class='layui-btn layui-btn-danger layui-btn-xs'>无效</button>";
                        }
                        return "<button class='layui-btn layui-btn-normal layui-btn-xs'>有效</button>";
                    }
                }
                , {
                    field: 'createTime',
                    align: 'center',
                    title: '创建时间',
                    sort: true,
                    minWidth: '200',
                    templet: function (d) {
                        return new Date(d.createTime).Format("yyyy-MM-dd hh:mm:ss");
                    }
                }
                , {title: '操作', align: 'center', toolbar: '#barOption'}
            ]]
            , id: 'dataCheck'
            , url: [[${basePath}]] + '/goods/list'
            , method: 'get'
            , page: true
            , limits: [10, 30, 60, 90, 150, 300]
            , toolbar: '#topOperate'
            , limit: 10 //默认采用0
            , loading: true
            // , where: {name: $("input[name='name']").val()}
            , done: function (res, curr, count) {
                //如果是异步请求数据方式，res即为你接口返回的信息。
                //如果是直接赋值的方式，res即为：{data: [], count: 99} data为当前页数据、count为数据总长度
                $("td[data-field='content']").each(function () {
                    $(this).find(".layui-table-cell").removeClass('layui-table-cell');
                });


            }
        });


        //头工具栏事件
        table.on('toolbar(event)', function (obj) {
            var checkStatus = table.checkStatus(obj.config.id);
            var data = checkStatus.data;

            switch (obj.event) {
                case 'toSave':
                    layer.open({
                        type: 2,
                        title: '新增商品',
                        shadeClose: true,
                        shade: 0.2,
                        maxmin: true, //开启最大化最小化按钮
                        area: ['1000px', '650px'],
                        content: [[${basePath}]] + '/goods/page/add'
                    });
                    break;
            }
        });

        table.on('tool(event)', function (obj) {
            var data = obj.data;
            if (obj.event === 'edit') {
                layer.open({
                    type: 2,
                    title: '编辑商品',
                    shadeClose: true,
                    shade: 0.2,
                    maxmin: true, //开启最大化最小化按钮
                    area: ['1000px', '650px'],
                    content: [[${basePath}]] + '/goods/page/edit?id=' + data.id
                });

            }
            if (obj.event === 'del') {
                layer.confirm('确定删除该商品?', {icon: 3, title: '提示'}, function (index) {
                    $.ajax({
                        type: "post",
                        url: [[${basePath}]] + "/goods/delGoods",
                        data: {
                            goodsId: data.id
                        },
                        success: function (data) {
                            if (data.success) {
                                tableIns.reload();
                                layer.msg("删除成功！", {icon: 1});
                            }
                        }
                    });
                    layer.close(index);
                });
            }
        });

        var type=null;
        form.on('select(type)', function(data){
            console.log(data.elem); //得到select原始DOM对象
            type=(data.value); //得到被选中的值
            console.log(data.othis); //得到美化后的DOM对象
        });




        $("#search").click(function () {
            tableIns.reload({
                where: {
                    title: $("input[name='title']").val()
                    ,sale: $("#saleId").find("option:selected").val()
                    ,status: $("#statusId").find("option:selected").val()
                },
                page: {
                    curr: 1
                }
            });
        })


    });
</script>
<!-- 表格操作按钮集 -->
<script type="text/html" id="barOption">
    <a class="layui-btn layui-btn-normal layui-btn-xs" lay-event="edit"><i class="layui-icon"></i>编辑</a>
    <a class="layui-btn layui-btn-danger layui-btn-xs" lay-event="del"><i class="layui-icon"></i>删除</a>
</script>
</body>
</html>